Constructor | |
---|---|
Menu(master, **option) | A menubar can be attach with main window by setting attribute menu=menubar. |
Options | |
---|---|
Title | |
Activebackground | |
Activeborderwidth | |
Activeforeground | |
Bg | |
cursor | |
font | |
fg | |
command | |
relief | |
image | |
selectcolor | |
tearoff |
|
Methods: | |
---|---|
add_command (options) | Adds a menu item to the menu. |
add_radiobutton( options ) | Adds a radio button menu item. |
add_checkbutton( options ) | Adds a check button menu item. |
add_cascade(options) | Adds a submenu. |
add_separator( ) | Adds a separator line. |
delete( startindex [, endindex ]) | Deletes the menu items ranging from startindex to endindex. |
entryconfig( index, options ) | Allows you to modify a menu item, which is identified by the index, and change its options. |
index(item) | Returns the index number of the given menu item label. |
insert_separator ( index ) | Insert a new separator at the position specified by index. |
invoke ( index ) | Calls the command callback associated with the choice at position index. If a checkbutton, its state is toggled between set and cleared; if a radiobutton, that choice is set. |
from tkinter import * class MyFrame(Tk): def __init__(self): super().__init__() self.geometry("400x300") self.mbar=Menu(self) self.config(menu=self.mbar) self.fmenu=Menu(self.mbar,tearoff=0) self.fmenu.add_command(label="New") self.fmenu.add_command(label="Open") self.fmenu.add_command(label="Save") self.fmenu.add_separator() self.fmenu.add_command(label="Exit",command=self.exit) self.mbar.add_cascade(label="File",menu=self.fmenu) self.emenu=Menu(self.mbar,tearoff=0) self.emenu.add_command(label="Undo") self.emenu.add_separator() self.emenu.add_command(label="Cut") self.emenu.add_command(label="Copy") self.emenu.add_command(label="Paste") self.emenu.add_command(label="Select All") self.mbar.add_cascade(label="Edit",menu=self.emenu) def exit(self): self.destroy() frm=MyFrame() frm.mainloop()